package com.dlc.garbagerecyclebox.bean;


import com.dlc.commonbiz.base.serialport.utils.ByteUtil;
import com.dlc.garbagerecyclebox.GarbageRecyclerBoxProtocol;
import com.dlc.garbagerecyclebox.SwitchType;

/**
 * @author :      fangbingran
 * @aescription : 管理维护信息
 * @date :        2019/08/01  20:00
 */
public class ManageMaintainInfoBean extends DeviceInfoBean {
    /**
     * 执行动作
     */
    private byte executeAction;
    private SwitchUpperHatchDoorStatus switchUpperHatchDoorStatus;
    /**
     * 执行状态
     */
    private ExecutionStatus executionStatus;
    private IOPortControlStatus ioPortControlStatus;
    private ConveyorBeltStatus conveyorBeltstatus;
    private PhotoRecognitionStatus photoRecognitionStatus;

    /**
     * 开关上舱门
     *
     * @return
     */
    public boolean isSwitchUpperHatchDoor() {
        return executeAction == GarbageRecyclerBoxProtocol.DeliveryCmd.CMD_WEIGHING_BEFORE_OPEN_DOOR;
    }

    public ManageMaintainInfoBean(DeviceInfoBean deviceInfoBean) {
        super(deviceInfoBean);
    }

    public ConveyorBeltStatus getConveyorBeltstatus() {
        return conveyorBeltstatus;
    }

    public void setConveyorBeltstatus(ConveyorBeltStatus conveyorBeltstatus) {
        this.conveyorBeltstatus = conveyorBeltstatus;
    }

    public PhotoRecognitionStatus getPhotoRecognitionStatus() {
        return photoRecognitionStatus;
    }

    public void setPhotoRecognitionStatus(PhotoRecognitionStatus photoRecognitionStatus) {
        this.photoRecognitionStatus = photoRecognitionStatus;
    }

    /**
     * 开下舱门
     *
     * @return
     */
    public boolean isOpenHatchDoor() {
        return executeAction == GarbageRecyclerBoxProtocol.DeliveryCmd.CMD_OPEN_HATCH_DOOR;
    }

    /**
     * IO口控制
     *
     * @return
     */
    public boolean isIOPortControl() {
        return executeAction == GarbageRecyclerBoxProtocol.DeliveryCmd.CMD_FIRST_PHOTOELECTRIC_TRIGGER;
    }

    /**
     * 传送带启停
     *
     * @return
     */
    public boolean isOpenCloseConveyorBelt() {
        return executeAction == GarbageRecyclerBoxProtocol.DeliveryCmd.CMD_TWO_PHOTOELECTRIC_TRIGGER;
    }

    /**
     * 主柜：拍照识别
     *
     * @return
     */
    public boolean isPhotoRecognition() {
        return executeAction == GarbageRecyclerBoxProtocol.DeliveryCmd.CMD_PHOTO_RECOGNITION;
    }

    /**
     * 电子秤置零
     *
     * @return
     */
    public boolean isElectronicScaleZero() {
        return executeAction == GarbageRecyclerBoxProtocol.DeliveryCmd.CMD_THREE_PHOTOELECTRIC_TRIGGER;
    }

    /**
     * 电子秤零点标定
     *
     * @return
     */
    public boolean isElectronicScaleZeroCalibration() {
        return executeAction == GarbageRecyclerBoxProtocol.DeliveryCmd.CMD_RETURN_FIRST_PHOTOELECTRIC_TRIGGER;
    }

    /**
     * 电子秤重量标定
     *
     * @return
     */
    public boolean isElectronicWeightCalibration() {
        return executeAction == GarbageRecyclerBoxProtocol.DeliveryCmd.CMD_WAITING_REMOVAL_EXCEPTION;
    }


    /**
     * 清空累计数量/重量命令
     *
     * @return
     */
    public boolean isEmptyAmount() {
        return executeAction == GarbageRecyclerBoxProtocol.DeliveryCmd.CMD_WAITING_REMOVAL_EXCEPTION2;
    }

    /**
     * 是否开启有害门
     *
     * @return
     */
    public boolean isOpenHarmfulWaste() {
        return executeAction == GarbageRecyclerBoxProtocol.CMD_OPEN_HARMFUL_WASTE;
    }

    @Override
    public String toString() {
        return "管理维护信息 ,状态命令:" + getStrDeliveryCmd() + ",说明:" + geExplain() + "," + super.toString() +
                (switchUpperHatchDoorStatus != null ? "," + switchUpperHatchDoorStatus.toString() : "") +
                (executionStatus != null ? "," + executionStatus.toString() : "") +
                (ioPortControlStatus != null ? "," + ioPortControlStatus.toString() : "") +
                (conveyorBeltstatus != null ? "," + conveyorBeltstatus.toString() : "") +
                (photoRecognitionStatus != null ? "," + photoRecognitionStatus.toString() : "");
    }


    private String geExplain() {
        if (isSwitchUpperHatchDoor()) {
            return "开关上舱门";
        }
        if (isOpenHatchDoor()) {
            return "开上舱门";
        }
        if (isIOPortControl()) {
            return "IO口控制";
        }
        if (isOpenCloseConveyorBelt()) {
            return "传送带启停";
        }
        if (isPhotoRecognition()) {
            return "拍照识别";
        }
        if (isElectronicScaleZero()) {
            return "电子秤置零";
        }
        if (isElectronicScaleZeroCalibration()) {
            return "电子秤零点标定";
        }
        if (isElectronicWeightCalibration()) {
            return "电子秤重量标定";
        }
        if (isEmptyAmount()) {
            return "清空累计数量/重量命令";
        }
        if (isOpenHarmfulWaste()) {
            return "开启有害门";
        }
        return "未知状态:";
    }


    public String getStrDeliveryCmd() {
        return ByteUtil.bytes2HexStr(new byte[]{
                executeAction});
    }

    public ExecutionStatus getExecutionStatus() {
        return executionStatus;
    }

    public void setExecutionStatus(ExecutionStatus executionStatus) {
        this.executionStatus = executionStatus;
    }

    public void setExecuteAction(byte executeAction, byte[] resultHex) {
        this.executeAction = executeAction;
        if (isSwitchUpperHatchDoor()) {
            switchUpperHatchDoorStatus = new SwitchUpperHatchDoorStatus();
            switchUpperHatchDoorStatus.setStatus(ByteUtil.byteToInt(new byte[]{resultHex[0]}));
            executionStatus = new ExecutionStatus(ByteUtil.byteToInt(new byte[]{resultHex[1]}));
            switchUpperHatchDoorStatus.setErrorCode(ByteUtil.byteToInt(new byte[]{resultHex[2]}));
            return;
        }
        if (isOpenCloseConveyorBelt()) {
            conveyorBeltstatus = new ConveyorBeltStatus();
            conveyorBeltstatus.setStartupStatus(ByteUtil.byteToInt(new byte[]{resultHex[0]}));
            conveyorBeltstatus.setErrorCode(ByteUtil.byteToInt(new byte[]{resultHex[2]}));
            executionStatus = new ExecutionStatus(ByteUtil.byteToInt(new byte[]{resultHex[1]}));
            return;
        }
        if (isPhotoRecognition()) {
            photoRecognitionStatus = new PhotoRecognitionStatus();
            photoRecognitionStatus.setPhotoRecognitionStatus(ByteUtil.byteToInt(new byte[]{resultHex[0]}));
        }
        if (isIOPortControl()) {
            ioPortControlStatus = new IOPortControlStatus();
            ioPortControlStatus.setIOType(ByteUtil.byteToInt(new byte[]{resultHex[0]}));
            ioPortControlStatus.setSwitchType(ByteUtil.byteToInt(new byte[]{resultHex[1]}));
            executionStatus = new ExecutionStatus(ByteUtil.byteToInt(new byte[]{resultHex[2]}));
            return;
        }
        if (isOpenHatchDoor() || isElectronicScaleZero() || isElectronicScaleZeroCalibration()
                || isElectronicWeightCalibration() || isEmptyAmount() || isOpenHarmfulWaste()) {
            executionStatus = new ExecutionStatus(ByteUtil.byteToInt(new byte[]{resultHex[0]}));
            return;
        }


    }


    public SwitchUpperHatchDoorStatus getSwitchUpperHatchDoorStatus() {
        return switchUpperHatchDoorStatus;
    }

    public void setSwitchUpperHatchDoorStatus(SwitchUpperHatchDoorStatus switchUpperHatchDoorStatus) {
        this.switchUpperHatchDoorStatus = switchUpperHatchDoorStatus;
    }

    public IOPortControlStatus getIoPortControlStatus() {
        return ioPortControlStatus;
    }

    public void setIoPortControlStatus(IOPortControlStatus ioPortControlStatus) {
        this.ioPortControlStatus = ioPortControlStatus;
    }

    public ConveyorBeltStatus getConveyorBeltStatus() {
        return conveyorBeltstatus;
    }

    public void setConveyorBeltStatus(ConveyorBeltStatus conveyorBeltstatus) {
        this.conveyorBeltstatus = conveyorBeltstatus;
    }

    /**
     * 拍照识别
     */
    public class PhotoRecognitionStatus implements Cloneable {
        private int photoRecognitionStatus;

        public int getPhotoRecognitionStatus() {
            return photoRecognitionStatus;
        }

        public void setPhotoRecognitionStatus(int photoRecognitionStatus) {
            this.photoRecognitionStatus = photoRecognitionStatus;
        }

        @Override
        public String toString() {
            return "拍照识别状态:" + geExplain();
        }

        public boolean isRecognition() {
            return photoRecognitionStatus == 0;
        }

        public boolean isRecognitionPasses() {
            return photoRecognitionStatus == 1;
        }

        public boolean isRecognitionFailure() {
            return photoRecognitionStatus == 2;
        }

        private String geExplain() {
            if (isRecognition()) {
                return "识别中";
            }
            if (isRecognitionPasses()) {
                return "识别通过";
            }
            if (isRecognitionFailure()) {
                return "识别失败";
            }
            return "未知识别状态:" + photoRecognitionStatus;
        }

        @Override
        public PhotoRecognitionStatus clone() throws CloneNotSupportedException {
            return (PhotoRecognitionStatus) super.clone();
        }
    }

    /**
     * 执行状态
     */
    public class ExecutionStatus implements Cloneable {
        private int executionStatus;

        public ExecutionStatus(int executionStatus) {
            this.executionStatus = executionStatus;
        }

        public boolean isExecutionProgress() {
            return executionStatus == 0;
        }

        public boolean isExecutionCompletion() {
            return executionStatus != 0;
        }

        public boolean isExecutionFailure() {
            return executionStatus == 2;
        }

        @Override
        public String toString() {
            return "执行状态:" + geExplain();
        }

        private String geExplain() {
            if (isExecutionProgress()) {
                return "执行中";
            }
            if (isExecutionCompletion()) {
                return "执行完成";
            }
            if (isExecutionFailure()) {
                return "执行失败";
            }
            return "未知执行状态:" + executionStatus;
        }

        @Override
        public ExecutionStatus clone() throws CloneNotSupportedException {
            return (ExecutionStatus) super.clone();
        }
    }

    /**
     * 启停传送带状态
     */
    public class ConveyorBeltStatus implements Cloneable {
        /**
         * 启动状态
         */
        private int startupStatus;
        /**
         * 错误码
         */
        private int errorCode;

        public int getStartupStatus() {
            return startupStatus;
        }

        public void setStartupStatus(int startupStatus) {
            this.startupStatus = startupStatus;
        }

        public int getErrorCode() {
            return errorCode;
        }

        public void setErrorCode(int errorCode) {
            this.errorCode = errorCode;
        }

        @Override
        public String toString() {
            return "开关上舱门状态,启动状态:" + getStartupStatusExplain() + ",错误码:" + geExplain();
        }

        public boolean isStop() {
            return startupStatus == 0;
        }

        public boolean isForwardStart() {
            return startupStatus == 1;
        }

        public boolean isReverseStartup() {
            return startupStatus == 2;
        }

        private String getStartupStatusExplain() {
            if (isStop()) {
                return "是否停止";
            }
            if (isForwardStart()) {
                return "正向启动";
            }
            if (isReverseStartup()) {
                return "反向启动";
            }
            return "未知启动状态:" + startupStatus;
        }

        private String geExplain() {

            if (isNormal()) {
                return "正常";
            }

            if (isMotorFault()) {
                return "电机故障";
            }
            return "未知错误:" + errorCode;
        }

        public boolean isMotorFault() {
            return errorCode == 3;
        }

        private boolean isNormal() {
            return errorCode == 0;
        }

        @Override
        public ConveyorBeltStatus clone() throws CloneNotSupportedException {
            return (ConveyorBeltStatus) super.clone();
        }
    }

    /**
     * IO控制状态
     */
    public class IOPortControlStatus implements Cloneable {
        /**
         * IO类型
         */
        private int IOType;

        private int switchType;

        public int getIOType() {
            return IOType;
        }

        public void setIOType(int IOType) {
            this.IOType = IOType;
        }

        public boolean isOpen() {
            return switchType == SwitchType.Open;
        }


        public boolean isOpenConveyorBeltExposureLamp() {
            return getIOType() == 1;
        }

        public boolean isLightingLampInBox() {
            return getIOType() == 2;
        }

        public boolean isFanInBox() {
            return getIOType() == 3;
        }

        public boolean isBoxTopIndicatorWhite() {
            return getIOType() == 4;
        }

        public boolean isBoxTopIndicatorGreen() {
            return getIOType() == 5;
        }

        public boolean isBoxTopIndicatorRed() {
            return getIOType() == 6;
        }

        private String geIOPortControlExplain() {
            if (isOpenConveyorBeltExposureLamp()) {
                return "是否打開传送带曝光灯";
            }
            if (isLightingLampInBox()) {
                return "箱内照明灯";
            }
            if (isFanInBox()) {
                return "箱内风扇";
            }
            if (isBoxTopIndicatorWhite()) {
                return "箱顶指示灯-白色";
            }
            if (isBoxTopIndicatorGreen()) {
                return "箱顶指示灯-绿色";
            }
            if (isBoxTopIndicatorRed()) {
                return "箱顶指示灯-红色";
            }
            return "未知IO类型:" + getIOType();
        }

        @Override
        public String toString() {
            return "IO控制状态,开关状态:" + (isOpen() ? "开" : "关") + ",IO类型值:" + getSwitchType() + ",IO类型说明:" + geIOPortControlExplain();
        }

        public void setSwitchType(int switchType) {
            this.switchType = switchType;
        }

        public int getSwitchType() {
            return this.switchType;
        }

        @Override
        public IOPortControlStatus clone() throws CloneNotSupportedException {
            return (IOPortControlStatus) super.clone();
        }
    }

    /**
     * 开关上舱门状态
     */
    public class SwitchUpperHatchDoorStatus implements Cloneable {
        private int status;
        /**
         * 错误码
         */
        private int errorCode;

        public boolean isOpen() {
            return status == SwitchType.Open;
        }


        public int getErrorCode() {
            return errorCode;
        }

        public void setErrorCode(int errorCode) {
            this.errorCode = errorCode;
        }

        public boolean isNormal() {
            return errorCode == 0;
        }

        public boolean isOvertime() {
            return errorCode == 1;
        }

        public boolean isTooShort() {
            return errorCode == 2;
        }

        public boolean isMotorFault() {
            return errorCode == 3;
        }

        private String geExplain() {
            if (isNormal()) {
                return "正常";
            }
            if (isOvertime()) {
                return "超时";
            }
            if (isTooShort()) {
                return "过短";
            }
            if (isMotorFault()) {
                return "电机故障";
            }
            return "未知错误:" + errorCode;
        }

        @Override
        public String toString() {
            return "开关上舱门状态,开关状态:" + (isOpen() ? "开" : "关") + ",状态值:" + status + ",错误码:" + geExplain();
        }


        public int getStatus() {
            return status;
        }

        public void setStatus(int status) {
            this.status = status;
        }

        @Override
        public SwitchUpperHatchDoorStatus clone() throws CloneNotSupportedException {
            return (SwitchUpperHatchDoorStatus) super.clone();
        }

    }

    @Override
    public ManageMaintainInfoBean clone() {
        ManageMaintainInfoBean manageMaintainInfoBean = null;
        try {
            manageMaintainInfoBean = (ManageMaintainInfoBean) super.clone();

            if (switchUpperHatchDoorStatus != null) {
                manageMaintainInfoBean.setSwitchUpperHatchDoorStatus(switchUpperHatchDoorStatus.clone());
            }
            if (executionStatus != null) {
                manageMaintainInfoBean.setExecutionStatus(executionStatus.clone());
            }
            if (ioPortControlStatus != null) {
                manageMaintainInfoBean.setIoPortControlStatus(ioPortControlStatus.clone());
            }
            if (conveyorBeltstatus != null) {
                manageMaintainInfoBean.setConveyorBeltStatus(conveyorBeltstatus.clone());
            }
            if (photoRecognitionStatus != null) {
                manageMaintainInfoBean.setPhotoRecognitionStatus(photoRecognitionStatus.clone());
            }
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
        return manageMaintainInfoBean;
    }
}
